!function ($) {
    const $baseurl = "http://localhost/ygsc/php/";
    const Neiron = $("#neiron");
    let default_arr = []; //默认数组
    let arr = []; //排序后的数组
    let before = null;
    let after = null;
    $.ajax({
        url: $baseurl + "listdata.php",
        dataType: "json",
    }).done(function (data) {
        // console.log(data);
        let $str = '';
        $.each(data, function (index, val) {
            $str += `
       <li>
       <a href="detail.html?sid=${val.goods_id}" target="_blank">
       <img  class="lazy" data-original="${val.goods_big_logo}" width="200" height="200">
       <p class="biaoti">${val.goods_name}</p>
       </a>
       <div id="descrption">
           <p id="descrption_neiron">优惠为${val.goods_price}<span class="Normalprice">原价为${Math.round(val.goods_price * 2)}</span></p>
       </div>
       <button>加入购物车</button>  
   </li>
     `
        })
        Neiron.html($str);
        //添加懒加载,添加到渲染的列表下面
        $(function () {
            $("img.lazy").lazyload({ placeholder: "img/seckill.png", effect: "fadeIn" });
        });
        $("#neiron li").each(function (data) {
            default_arr[data] = $(this);
            arr[data] = $(this);
        })
        console.log(default_arr);

    })
    // 页码
    $('.page').pagination({
        pageCount: 47, //总的页数
        jump: true, //是否开启跳转到指定的页数，布尔值。
        coping: true, //是否开启首页和尾页，布尔值。
        prevContent: '上一页',
        nextContent: '下一页',
        homePage: '首页',
        endPage: '尾页',
        callback: function (api) {
            $.ajax({
                url: $baseurl + "listdata.php",
                dataType: "json",
                data: {
                    page: api.getCurrent()
                }
            }).done(function (data) {
                let $str = '';
                console.log(data);
                $.each(data, function (index, val) {
                    $str += `
            <li>
            <a href="detail.html?sid=${val.goods_id}" target="_blank">
            <img  class="lazy" data-original="${val.goods_big_logo}" width="200" height="200">
            <p class="biaoti">${val.goods_name}</p>
            </a>
            <div id="descrption">
                <p id="descrption_neiron">优惠为${val.goods_price}<span class="Normalprice">原价为${Math.round(val.goods_price * 2)}</span></p>
            </div>
            <button>加入购物车</button>  
        </li>
        `
                })
                // console.log($str);
                Neiron.html($str);
                $(function () {
                    $("img.lazy").lazyload({ placeholder: "img/seckill.png", effect: "fadeIn" });
                });
                $("#neiron li").each(function (data) {
                    default_arr[data] = $(this);
                    arr[data] = $(this);

                })
                console.log(default_arr);

            })
        }

    })

    $("#daohang p").eq(2).on("click", function () {
        $.each(default_arr, function (index, val) {
            $("#neiron").append(val);
        })

    })
    $("#daohang p").eq(0).on("click", function () {  //升序
        for (let i = 0; i < arr.length - 1; i++) {  //最大长度
            for (let j = 0; j < arr.length -i -1; j++) {
               before = parseInt(arr[j].find(".Normalprice").html().substring(3));
               after = parseInt(arr[j+1].find(".Normalprice").html().substring(3));
            //    console.log(before);
            if (before >after) {
                let temp =arr[j];
                arr[j] =arr[j+1];
                arr[j+1] =temp;
            }
            }

        }
        $.each(arr,function(index,val){
            $("#neiron").append(val)
        })

    })

    $("#daohang p").eq(1).on("click", function () {  //升序
        for (let i = 0; i < arr.length - 1; i++) {  //最大长度
            for (let j = 0; j < arr.length -i -1; j++) {
               before = parseInt(arr[j].find(".Normalprice").html().substring(3));
               after = parseInt(arr[j+1].find(".Normalprice").html().substring(3));
            //    console.log(before);
            if (before < after) {
                let temp =arr[j+1];
                arr[j+1] =arr[j];
                arr[j] =temp;
            }
            }

        }
        $.each(arr,function(index,val){
            $("#neiron").append(val)
        })

    })

    //点击事件
}(jQuery);